package com.vastcom.themeleaf.web;

import com.vastcom.themeleaf.model.User;
import com.vastcom.themeleaf.vo.UserVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

@Controller

public class PublicController {

@RequestMapping("/")
    public String index(Model model){
    List<User> ul=new ArrayList<User>();
    User u=new User();
    u.setPassword("1234");
    u.setUsername("admin");
    u.setAge(20);
    u.setPrice(15.65);
    u.setBirth(new Timestamp(System.currentTimeMillis()));
    ul.add(u);
    User u1=new User();
    u1.setPassword("12341");
    u1.setUsername("admin1");
    u1.setAge(21);
    u1.setPrice(11.65);
    u1.setBirth(new Timestamp(System.currentTimeMillis()));
ul.add(u1);
    String userStr="henry";


    model.addAttribute("username",userStr);
    model.addAttribute("user",u);
    model.addAttribute("ul",ul);
    return "/admin";
}

@RequestMapping("addform")
    public String addform(){
        return "/addform";
}





@RequestMapping("toupdate")
public String toupdate( Model model){
    User u=new User();
    u.setPassword("1234");
    u.setUsername("admin");
    u.setAge(20);
    u.setPrice(15.65);
    u.setBirth(new Timestamp(System.currentTimeMillis()));
    model.addAttribute("u",u);
        return "/updateform";
}

@PostMapping("saveForm")
    public String saveForm(@ModelAttribute UserVo u){

    System.out.printf("username"+u.getUsername());
    System.out.printf("password"+u.getPassword());
    System.out.printf("age"+u.getAge());
    System.out.printf("price"+u.getPrice());
    System.out.printf("birth"+u.getBirth());


    return "redirect:/toupdate";



}

@PostMapping("saveUser")
@ResponseBody
    public User saveUser(@RequestBody User u){

        User t=new User();
        t.setUsername(u.getUsername());
return t;
}



}
